define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xapply/app/index' + location.search,
                    add_url: 'xapply/app/add',
                    edit_url: 'xapply/app/edit',
                    del_url: 'xapply/app/del',
                    multi_url: 'xapply/app/multi',
                    import_url: 'xapply/app/import',
                    table: 'xapply_app',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'category.name', title: __('Category.name'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'app_name', title: __('App_name'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'is_use', title: __('Is_use'), searchList: {"0":__('Is_use 0'),"1":__('Is_use 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_notice', title: __('Is_notice'), searchList: {"0":__('Is_notice 0'),"1":__('Is_notice 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_admin', title: __('Is_admin'), searchList: {"0":__('Is_admin 0'),"1":__('Is_admin 1')}, formatter: Table.api.formatter.normal},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                            {
                                name: 'use',
                                text: '使用权限',
                                classname: 'btn btn-info btn-xs btn-dialog',
                                url: 'xapply/rule/add/ids/{ids}/type/0'
                            },
                            {
                                name: 'notify',
                                text: '通知权限',
                                classname: 'btn btn-info btn-xs btn-dialog',
                                url: 'xapply/rule/add/ids/{ids}/type/1'
                            },
                            {
                                name: 'manage',
                                text: '管理权限',
                                classname: 'btn btn-info btn-xs btn-dialog',
                                url: 'xapply/rule/add/ids/{ids}/type/2'
                            },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
